//
//  TabbarViewController.m
//  WChat
//
//  Created by lw on 2018/7/28.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

#import "TabbarViewController.h"
#import "ContactTableViewController.h"

#define OPENCENTERX 220.0
#define DIVIDWIDTH 70.0 //OPENCENTERX 对应确认是否打开或关闭的分界线
CGPoint openPointCenter;
@interface TabbarViewController ()<IChatManagerDelegate>{

    CGPoint closePointCenter;
}
@property (strong, nonatomic) UIViewController *parentController;
@end

@implementation TabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    [self setup];
    [self addGesture];
}

- (void)addGesture {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
//    [self.view addGestureRecognizer:tap];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.view addGestureRecognizer:pan];
    NSLog(@"openPointCenter x:%f, openPointCenter y:%f", openPointCenter.x, openPointCenter.y);
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer {
    [UIView animateWithDuration:0.75
                          delay:0.01
                        options:UIViewAnimationTransitionCurlUp animations:^(void){
                            self.view.center = CGPointMake(openPointCenter.x - OPENCENTERX,
                                                      openPointCenter.y);
                        }completion:nil];
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
    
    CGPoint translation = [recognizer translationInView:self.view.superview];
    
    float x = self.view.center.x + translation.x;
    NSLog(@"translation x:%f", translation.x);
    
    if (x < self.view.superview.center.x) {
        x = self.view.superview.center.x;
    }
    self.view.center = CGPointMake(x, openPointCenter.y);
    if(recognizer.state == UIGestureRecognizerStateEnded) {
        [UIView animateWithDuration:0.75
                              delay:0.01
                            options:UIViewAnimationCurveEaseInOut
                         animations:^(void) {
             if (x > openPointCenter.x - DIVIDWIDTH) {
                 self.view.center = openPointCenter;
             }else{
                 self.view.center = CGPointMake(openPointCenter.x - OPENCENTERX,
                                           openPointCenter.y);
             }
         }completion:^(BOOL isFinish){
         }];
    }
    [recognizer setTranslation:CGPointZero inView:self.view.superview];
}

- (void)setup {
    [self.view.layer setShadowOffset:CGSizeMake(10, 10)];
    [self.view.layer setShadowRadius:20];
    [self.view.layer setShadowOpacity:1];
    [self.view.layer setShadowColor:[UIColor blackColor].CGColor];
    UIColor *selColor = [UIColor colorWithRed:0 green:190/255.0 blue:12/255.0 alpha:1];
    for (UINavigationController *nav in self.childViewControllers) {
//        [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
//        [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateSelected];
    }
    self.tabBar.tintColor = selColor;
}

+ (instancetype)initWithParentController:(UIViewController *)parentController {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    TabbarViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:NSStringFromClass([TabbarViewController class])];
    if (parentController) {
        [parentController addChildViewController:vc];
        [parentController.view addSubview:vc.view];
        openPointCenter = CGPointMake(parentController.view.center.x + OPENCENTERX, parentController.view.center.y);
    }
    return vc;
}

- (void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message {
    NSLog(@"有好友请求: %@==%@",username, message);
    
    ContactTableViewController *vc = self.childViewControllers[2];
    NSInteger badgeValue = [vc.navigationController.tabBarItem.badgeValue integerValue];
    badgeValue++;
    vc.navigationController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",badgeValue];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"好友:",username] message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *agree = [UIAlertAction actionWithTitle:@"同意" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        EMError *error = nil;
        BOOL isSuccess = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:username error:&error];
        if (isSuccess && !error) {
            NSLog(@"发送同意成功");
            ContactTableViewController *vc = self.childViewControllers[1];
            [self.tabBar setSelectedItem:vc.navigationController.tabBarItem];
//            self.selectedViewController = vc;
        }
    }];
    [alert addAction:agree];
    UIAlertAction *cencel = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        EMError *error = nil;
        BOOL isSuccess = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:username reason:@"不同意" error:&error];
        if (isSuccess && !error) {
            NSLog(@"拒绝成功");
        }
    }];
    [alert addAction:cencel];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
